import {BaseUrl} from "../constant/constant";
import {ApiFetch} from "../api/apiHandler";
const log = console.log.bind(console)

class MallModel {

    constructor() {
        this.url = BaseUrl
    }

    add(params) {
        let end = this.url + 'addproduct'
        let fm = new FormData()
        Object.entries(params).map(([key, value]) => {
            if (value !== undefined && value !== null) {
                if (key === 'image') {
                    const imgs = []
                    value.map(img => {
                        if (typeof img === 'string') {
                            imgs.push(img)
                        } else {
                            log(img)
                            imgs.push(img.name)
                            fm.append(img.name, img.originFileObj)
                        }
                    })
                    fm.append('image', JSON.stringify(imgs))
                } else {
                    fm.append(key, value)
                }
            }

        })
        let req = new ApiFetch(end)
        return req.fetch(fm)
    }

    all(params) {
        let end = this.url + 'getproducts'
        let req = new ApiFetch(end)
        return req.fetch(params)
    }

    hot(params) {
        let end = this.url + 'getproductshot'
        let req = new ApiFetch(end)
        return req.fetch(params)
    }

    setHot(params) {
        let end = this.url + 'sethotproduct'
        let req = new ApiFetch(end)
        return req.fetch(params)
    }

    action(params) {
        let end = this.url + 'finishproductdeal'
        let req = new ApiFetch(end)
        return req.fetch(params)
    }

    deal(params) {
        let end = this.url + 'getproductdeal'
        let req = new ApiFetch(end)
        return req.fetch(params)
    }

    delete(params) {
        let end = this.url + 'delproductdeal'
        let req = new ApiFetch(end)
        return req.fetch(params)
    }

    send(params) {
        let end = this.url + 'finishproductdeal'
        let req = new ApiFetch(end)
        return req.fetch(params)
    }

}

export default MallModel
